﻿using CommonTools;
using shop.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace shop.Bll.BasicInfo
{
    public class JSupplierBll : BasicBllParent, IFastQueryBll<JSupplier>
    {
        #region 供应商表

        public override IEnumerable<object> Query(string code, bool IsDetails, out string Msg)
        {
            Msg = null;

            var Supplier = DB.FSql.Select<JSupplier>().Where(s => s.IsDetails == IsDetails);
            if (string.IsNullOrEmpty(code) == false)
            {
                Supplier = Supplier.Where(a => a.Code.StartsWith(code));
            }

            return Supplier.ToList();

        }

        public override bool AddGroup(object obj, IUser user, out string Msg)
        {
            Msg = null;
            JSupplier Supplier = obj as JSupplier;
            if (Supplier == null)
            {
                Msg = "b:供应商表为空";
                return false;
            }

            var id = DB.FSql.Insert(Supplier).ExecuteIdentity();
            Supplier.ID = (int)id;

             
            Msg = "添加成功";
            return true;
 
        }

        public override bool DeleteGroup(object obj, IUser user, out string Msg)
        {
            Msg = null;
            JSupplier Supplier = obj as JSupplier;
            if (Supplier == null)
            {
                Msg = "b:供应商表为空";
                return false;
            }

            if (DB.FSql.Select<JSupplier>()
                .Any(a => a.Isdel== false && a.ID!=Supplier.ID
                && a.Code.StartsWith(Supplier.Code+ this.Separator)))
            {
                //
                Msg = CN.Msg_GroupHasItemsCannotDelete;
                return false;
            }

            DB.FSql.Update<JSupplier>()
               .Where(a => a.Code == Supplier.Code && a.Isdel == false)
               .Set(a => a.Isdel, true)
               .ExecuteAffrows();
             
            return true;
        }

        /// <summary> 修改分组 </summary>
        public override bool UpdateGroup(object obj, IUser user, out string err)
        {
            err = "";
            if (!(obj is JSupplier model))
            {
                err = "辅助资料为空";
                return false;
            }

            throw new NotImplementedException();
            //return dal.ClassModify(model, user, out err);
        }

        public override bool Insert(object obj, IUser user, out string Msg)
        {
            Msg = null;
            JSupplier Supplier = obj as JSupplier;
            Supplier.Isdel = false;
            Supplier.IsDetails = true;
            Supplier.Biller = user.Name;

            if (Supplier == null)
            {
                Msg = "b:供应商表对象为空";
                return false;
            }

            if (this.GetByCode(Supplier.Code)!=null)
            {
                Msg = "b:供应商表代码重复";
                return false;
            }
            if (this.GetByName(Supplier.Name) != null)
            {
                Msg = "b:供应商表名称重复";
                return false;
            }
            if (Supplier.Code == null || Supplier.Code.Length <= 0)
            {
                Msg = "b:供应商表代码不能为空";
                return false;
            }
            if (Supplier.Name == null || Supplier.Name.Length <= 0)
            {
                Msg = "b:供应商表名称不能为空";
                return false;
            }
            
            var id = DB.FSql.Insert(Supplier).ExecuteIdentity();
            Supplier.ID = (int)id;

            return true;
        }

        public override bool Update(object obj, IUser user, out string Msg)
        {
            Msg = null;
            JSupplier Supplier = obj as JSupplier;
            if (Supplier == null)
            {
                Msg = "b:供应商表对象为空";
                return false;
            }
            if (Supplier.ID <= 0)
            {
                Msg = "b:供应商表ID不能为空";
                return false;
            }
            if (Supplier.Code == null || Supplier.Code.Length <= 0)
            {
                Msg = "b:供应商表代码不能为空";
                return false;
            }
            if (Supplier.Name == null || Supplier.Name.Length <= 0)
            {
                Msg = "b:供应商表名称不能为空";
                return false;
            }

            DB.FSql.Update<JSupplier>()
               .SetSource(Supplier)
               .ExecuteAffrows();

             
            return true;
           
        }

        public override bool Delete(object obj, IUser user, out string Msg)
        {
            Msg = null;
            JSupplier Supplier = obj as JSupplier;
            if (Supplier == null)
            {
                Msg = "b:供应商表对象为空";
                return false;
            }
            if (Supplier.ID <= 0)
            {
                Msg = "b:供应商表ID不能为空";
                return false;
            }
            Supplier.Isdel = true;
            Supplier.BillerDate = DateTime.Now;

            DB.FSql.Update<JSupplier>()
               .SetSource(Supplier)
               .Set(a => a.Isdel, true)
               .ExecuteAffrows();
             
            return true;
           
        }

        #endregion


        #region 实现IFastQueryBll 接口, 在FastBox中使用


        /// <summary> 在查询,报表中显式的标题名称 </summary>
        public string Title => "配件供应商";


        /// <summary> 本地存储的明细 </summary>
        /// <remarks>如果基础资每次都从数据库查询, 这个没用, 保持null</remarks>
        public List<JSupplier> Items => null;


        /// <summary> 是否每次从数据库查询 </summary>
        public bool QueryFromDataBase => true;


        /// <summary> 存储在快速查询窗体中,显示的属性,及属性的显示, 这个暂时不用,以后删除 </summary>
        public Dictionary<string, string> DisplayColumns => null;


        /// <summary> 显示的列, 静态 </summary>
        private static readonly string[] _columns = new string[] { nameof(JSupplier.Code), nameof(JSupplier.Name) };


        /// <summary> 存储在快速查询窗体中,显示的属性 </summary>
        public string[] Columns => _columns;


        /// <summary> 实体模型的代码属性名称 </summary>
        public string CodeField => nameof(JSupplier.Code);



        /// <summary> 代码中级别的分隔符 </summary>
        public override string Separator => ".";


 

        /// <summary> 查询树形图的所有节点 </summary>
        public override IEnumerable<IFastTree> QueryTreeList()
        {
            var rst = DB.FSql.Select<JSupplier>().Where(a => a.IsDetails == false && a.Isdel == false);

            return rst.ToList() ;
        }


        /// <summary> 查询方法 </summary>
        public List<JSupplier> Query(string findText, string prefixCode = null)
        {
            return DB.FSql.Select<JSupplier>()
                .Where(a => a.Isdel == false && a.IsDetails == true
                            && (a.Name.Contains(findText) || a.Code.Contains(findText)))
                  .ToList().OrderBy(a => a.Code)
                  .ToList();

        }


        /// <summary> 查询树形图的一个节点下的所有内容 </summary>
        public List<JSupplier> QueryByGroup(string group)
        {
            var list = DB.FSql.Select<JSupplier>()
                    .Where(w => w.IsDetails == true && w.Isdel == false && w.Code.StartsWith(group));

            return list.ToList().OrderBy(a => a.Code).ToList();        //排序
             
        }

        public override bool Check(object obj, IUser user, out string err)
        {
            throw new NotImplementedException();
        }

        public override bool UnCheck(object obj, IUser user, out string err)
        {
            throw new NotImplementedException();
        }


        #endregion


        #region 权限

        /// <summary> 查询导出权限 </summary>
        public override string LookRight => "01.02.1";

        /// <summary> 保存权限 </summary>
        public override string SaveRight => "01.02.2";

        /// <summary> 修改别人单据权限 </summary>
        public string ModifyRight => "01.02.3";

        /// <summary> 删除别人单据权限 </summary>
        public string DeleteRight => "01.02.4";

        /// <summary> 审核,反审核权限 </summary>
        public override string CheckRight => "01.02.5";

        #endregion



        #region 其他


        /// <summary> 使用代码查询一个 </summary>
        public JSupplier GetByCode(string code)
        {
            return DB.FSql.Select<JSupplier>()
                .Where(a => a.Isdel == false && a.IsDetails == true && a.Code == code)
                .ToOne();
        }

        public JSupplier GetByName(string name)
        {
            return DB.FSql.Select<JSupplier>()
                .Where(a => a.Isdel == false && a.IsDetails == true && a.Name == name)
                .ToOne();
        }

        

        #endregion
    }
}
